<script lang="ts" setup>
import { ref, watchEffect, nextTick, onMounted } from "vue";
import { mStyles } from "@/utils/useStyles";
import API from "@/service/api/faq";
import { request } from "@/service/request";
import { useStore } from "@/stores/user";
import { useRouter, useRoute } from "vue-router";
import { useFullPage } from "@/hook/useFullPage";
import { someFieldStore } from '@/stores/somefield';
import { getUrlParamValue } from "@/utils/getUrlParams";

defineOptions({
    name: "home"
})

//问题分类
interface FaqCategory {
    id: number;
    title: string;
    children: Array<{ id: number; title: string }>;
}
const mFaqList = ref<FaqCategory[]>([]);

const clkGetfaq = async () => {
    const { data } = await request.post<any>(API.faqList);
    mFaqList.value = data.children;
};
clkGetfaq()


//热门问题
const mFaqHotList = ref<any>([]);
const clkGetHotfaq = async () => {
    const { data } = await request.post<any>(API.faqHotList);
    mFaqHotList.value = data;
};
clkGetHotfaq()

//IDor立即登录享受更多服务
const mHeaderText = ref<string>('立即登录');

const mStore = useStore();

watchEffect(() => {
    if (mStore.state) {
        let user_name = mStore.token.user_name;
        let phone = mStore.token.user_name;
        mHeaderText.value = user_name ? user_name : phone;
    }
});

const mRouter = useRouter();
const mRoute = useRoute();

// 生成随机颜色
const getRandomColor = (seed?: number) => {
    if (typeof seed === 'number' && seed < 3) {
        return '#EA5A39';
    }
    return '#3D3D3D';
};

//联系客服需登录
const mActionSheetRef = ref();
const clkLoginKf = () => {
    if (!mStore.state) {
        mActionSheetRef.value.openDialog();
    } else {
        window.open(API.getKfUrl(), '_self');
    }
};


//切换账号
const mSomeField = someFieldStore();


// 控制展开/收起状态
const mExpandedTabs = ref<Record<number, boolean>>({});
const mActiveTab = ref<string | number>(0);
const mFixed = ref(false);


//动态获取导航栏占位符的高度
const placeholderHeight = ref('46px');
onMounted(() => {
    nextTick(() => {
        const navBar = document.querySelector('.van-nav-bar');
        if (navBar) {
            const actualHeight = (navBar as HTMLElement).offsetHeight - 2 + 'px';
            placeholderHeight.value = actualHeight;
        }
    });
});
//是否吸顶
const clkScroll = (e: { scrollTop: number, isFixed: boolean }) => {
    if (e.isFixed) {
        mFixed.value = true;
    } else {
        mFixed.value = false;
    }
}

const { pageRef } = useFullPage()

</script>

<template>
    <div class="gr-bg" ref="pageRef">
        <cNavbar :iFontColor="mStyles?.color10"
            :iBgColor="!mFixed ? 'transparent' : 'linear-gradient(135deg, #FF0000 0%, #FF3A37 100%)'"
            iFontSize="4.427vmin" class="nav" :iTitle="!mFixed ? '客服中心' : ''" :iFixed="mFixed" :iResetyle="mFixed">
            <template #right>
                <div class="gr-order" @click="mRouter.push('/order')">
                    <van-image src="/order-icon.png" width="4.688vmin" height="4.688vmin"></van-image>
                    <span class="text-[#ffff] text-[18px] m-t-[10px]">服务记录</span>
                </div>
            </template>
            <template #leftExtension>
                <transition>
                    <div class="gr-header-login-after navuser line-height-[30px] text-align-left"
                        v-show="mFixed == true && mStore.state">
                        <van-image src="/head.png" width="8vmin" height="8vmin" round class="m-r-[10px]"></van-image>
                        <div>
                            <h1 class="text-[26px] font-bold">{{ mHeaderText }}</h1>
                            <span
                                class="text-[18px] bg-[#920000]/20  color-[#ffff]/80 border-rd-[5px] p-[5px_12px_5px_12px]">
                                UID:{{
                                    mStore.token.uid }}</span>
                        </div>
                    </div>
                </transition>
            </template>
        </cNavbar>
        <div class="gr-header-login-before" v-if="!mStore.state">
            <p class="text-[40px] text-[#fff] font-bold m-b-[25px]">登录体验完整客服功能~</p>
            <cButton @click="mRouter.push('/login')" round iFontSize="3.646vmin" iColor="#FF0000" iHeight="5.333vmin"
                iPadding="5.333vmin" class="w-[222px] font-medium">{{ mHeaderText }}</cButton>
        </div>
        <transition>
            <div class="gr-header-login-after" v-show="mFixed == false && mStore.state">
                <van-image src="/head.png" width="11.733vmin" height="11.733vmin" round class="m-r-[20px]"></van-image>
                <div>
                    <h1 class="text-[40px] font-bold">{{ mHeaderText }}</h1>
                    <span
                        class="text-[20px] bg-[#920000]/20 color-[#ffff]/80 border-rd-[5px] m-t-[5px] p-[5px_12px_5px_12px] inline-block">
                        UID:
                        {{
                            mStore.token.uid
                        }}
                    </span>
                </div>
                <cButton v-show="getUrlParamValue('access_token', mSomeField.clientfields) == null && mStore.state" iHeight="6.333vmin" iPadding="0.553vmin 2vmin 0.553vmin 2vmin"
                    round class="m-l-[30px] w-[144px] text-[24px]" @click="mRouter.push({ path: '/login'})" iColor="#FF0000">切换账号</cButton>
            </div>
        </transition>
        <div class="gr-guess">
            <div class="gr-guess-header">
                <van-image src="/hot-icon.png" width="4.267vmin" height="4.267vmin" fit="contain"
                    class="m-r-[7px]"></van-image>
                <h1 class="font-bold">热门问题</h1>
            </div>
            <ul>
                <li v-for="(item, index) in mFaqHotList" :key="item.id">
                    <div>
                        <a @click="mRouter.push(`/detail?id=${item.id}&category=${item.parent_title}`)" class="gr-guess-item text-ellipsis">
                            <span class="m-r-[22px]" :style="{ color: getRandomColor(index) }">{{ index + 1 }}</span>
                            <van-text-ellipsis rows="1" :content="item.title"></van-text-ellipsis>
                        </a>
                    </div>
                </li>
            </ul>
        </div>
        <div class="gr-category">
            <van-tabs animated sticky @scroll="clkScroll" :offset-top="placeholderHeight" v-model:active="mActiveTab" :ellipsis="false">
                <van-tab :title="items['title']" v-for="(items, index) in mFaqList" :name="items['id']">
                    <ul>
                        <li v-for="(item, i) in (mExpandedTabs[items.id] ? items.children : items.children.slice(0, 5))"
                            @click="mRouter.push(`/detail?id=${item.id}&category=${items.title}`)">
                            {{ item['title'] }}
                            <span><van-icon name="arrow" color="#3D3D3D" /></span>
                        </li>
                    </ul>
                    <van-divider v-show="items.children.length > 5" />
                    <p class="p-b-[32px] flex items-center justify-center"
                        @click="mExpandedTabs[items.id] = !mExpandedTabs[items.id]" v-show="items.children.length > 5">
                        <span class="text-[#3D3D3D]/40 font-medium text-[26px]">
                            {{ mExpandedTabs[items.id] ? '收起' : '展开全部' }}
                        </span>
                        <van-icon name="arrow-down" color="rgba(61, 61, 61, 0.4)" size="3.133vmin"
                            :class="{ 'rotate-180': mExpandedTabs[items.id] }" />
                    </p>
                </van-tab>
            </van-tabs>
        </div>
        <cVanActionSheet ref="mActionSheetRef" :iButton="false" iPadding="6.25vmin 3.125vmin 8.854vmin 3.125vmin" iText="登录账号">
            <h1 class="text-[34px] font-medium">你还未登录，请先登录账号</h1>
            <cButton class="w-[100%] m-t-[40px] text-[28px] font-medium" iLinearGradien round iColor="white" iHeight="9.6vmin" @click="mRouter.push({ path: '/login', query: { redirect: mRoute.fullPath } })">登录账号</cButton>
        </cVanActionSheet>
        <van-tabbar class="gr-kf" safe-area-inset-bottom placeholder>
            <a @click="clkLoginKf">
                <van-image src="/kf.png" width="6.25vmin" ></van-image>
                <span class="text-[30px] text-[#3D3D3D] font-medium m-l-[10px]">联系客服</span>
            </a>
        </van-tabbar>
    </div>
</template>

<style lang="scss" scoped>
.nav {
    .navuser {
        margin: 0 0 0 10px;
    }
}

.rotate-18 {
    transform: rotate(18deg)
}

.gr-bg {
    background: url('/home-bg.png') no-repeat, #F6F6F6;
    background-size: 100%;
    padding: 0 24px 0 24px;
}

.gr-order {
    line-height: 16px;

    span {
        display: block;
    }
}

.gr-header-login-before {
    height: 150px;
    text-align: left;
    margin: 51px 0 61px 15px;
}

.gr-header-login-after {
    display: flex;
    margin: 51px 0 60px 15px;
    align-items: flex-start;
    justify-items: center;
    color: #ffff;
}

.gr-category {
    background: #ffff;
    padding: 20px 0px 0 0px;
    margin-bottom: 40px;
    margin-top: 20px;
    border-radius: 24px;

    ul {
        padding: 0 30px 0 30px;

        li {
            height: 100px;
            line-height: 100px;
            display: flex;
            justify-content: space-between;
            font-size: 30px;
            font-weight: 500;
        }
    }

    :deep(.van-tabs__wrap) {
        border-top-left-radius: 24px;
        border-top-right-radius: 24px;
        border-bottom: 1px solid rgba(61, 61, 61, 0.1);
    }

    :deep(.van-tab) {
        margin-right: 42px;
        color: rgba(61, 61, 61, 0.8);
        font-size: 32px;
        font-weight: 400;

        &:first-child {
            margin-left: 30px;
        }
    }

    :deep(.van-tab--active) {
        color: rgba(61, 61, 61, 1);
        font-weight: 700;
    }

    :deep(.van-tabs__nav--line) {
        padding: 0;
    }

    :deep(.van-tab--grow) {
        padding: 0;
    }

    :deep(.van-tabs__line) {
        background: #F90919;
        width: 48px;
        bottom: 5px;
    }
}

.gr-guess {
    background: white;
    margin: 10px auto;
    border-radius: 24px;

    .gr-guess-header {
        padding: 32px 40px 32px 40px;
        display: flex;
        align-items: center;
        margin-bottom: 10px;
        border-bottom: 1px solid rgba(61, 61, 61, 0.10);
    }

    ul {
        padding: 22px 40px 10px 40px;
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(230px, 2fr));
        gap: 10px;

        li {
            margin-bottom: 20px;

            div {
                width: 100%;
                display: flex;
                font-weight: 500;
                align-items: center;
            }

            &::marker {
                color: $color3;
            }

            a {
                display: contents;
                font-size: 30px;
                font-weight: 500;
            }
        }
    }


    h1 {
        font-size: 32px;
        margin: 10px 0 10px 0;
    }
}

.gr-kf {
    :deep(.van-tabbar--fixed) {
        justify-content: center;
        padding: 20px 0 20px 0;
    }

    a {
        color: #7bc7ff;
        display: flex;
        align-items: center;
    }
}
</style>